package com.symbio.ishenapi.doctor.controller;

import com.symbio.ishenapi.common.bean.LoginUser;
import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.doctor.model.TmBrandMaterials;
import io.jboot.web.controller.annotation.RequestMapping;

import java.math.BigInteger;
import java.util.Date;
import java.util.Map;

/**
 * 个人品牌物料申请
 */
@RequestMapping("/brandMaterialsController")
public class TmBrandMaterialsController extends BaseController {

    /**
     * 查询全部申请信息
     */
    public void findTmBrandMaterials(){
       /* TmBrandMaterials user = (TmBrandMaterials)getReqBodyObject(TmBrandMaterials.class);
        List<TmBrandMaterials> list=TmBrandMaterials.dao.findTmBrandMaterials(user);
        renderJsonRet(list);*/
        String doctorName = getPara("doctorName");

        LoginUser loginUser = getLoginUser();
        String Ids = loginUser.getDoctorIds();
        if (Ids.endsWith(",")) {
            Ids = Ids.substring(0,(Ids.length() - 1));
        }

        Map map = TmBrandMaterials.dao.findTmBrandMaterials(doctorName,getQueryModel(),Ids);
        renderPageJsonRet(map.get("count"),map.get("data"));
    }


    /**
     * 根据医生姓名和手机号模糊查询
     */
    public void findByLike(){
        String doctorName = getPara("doctorName");
        if(doctorName == null){
            renderJsonError(40003, "医生姓名或手机号");
        }
        //BigInteger doctorId = new BigInteger(getPara("doctorId"));
        Map map = TmBrandMaterials.dao.findByLike(doctorName,getQueryModel());
        renderPageJsonRet(map.get("count"),map.get("data"));
    }


    /**
     * 根据医生id查询医生的申请记录
     */
    public void findDoctorIdByDoctor(){

        BigInteger doctorId = new BigInteger(getPara("doctorId"));
        if(doctorId == null){
            renderJsonError(40003, "医生编号");
        }
        Map map = TmBrandMaterials.dao.findDoctorIdByDoctor(doctorId,getQueryModel());
        renderPageJsonRet(map.get("count"),map.get("data"));

    }

    /**
     * 根据申请id获取数据
     */
    public void findById(){
       // System.out.println(getReqBodyString());
        TmBrandMaterials user = (TmBrandMaterials)getReqBodyObject(TmBrandMaterials.class);
        if(user.getId() == null || "".equals( user.getId())){
            renderJsonError(40003, "申请编号");
        }
        TmBrandMaterials tmBrandMaterials = TmBrandMaterials.dao.findById(user.getId());
        renderJsonRet(tmBrandMaterials);
    }


    /**
     * 修改申请状态
     */
    public void updateStatus() {
        TmBrandMaterials user = (TmBrandMaterials) getReqBodyObject(TmBrandMaterials.class);
        TmBrandMaterials u = TmBrandMaterials.dao.findById(user.getId());
        if (u != null) {
            u.setStatus(user.getStatus());
            u.setUpdateTime(new Date());
            if (u.update()) {
                renderJsonRet(user.getId());
            } else {
                renderJsonError(40005);
            }
        } else {
            renderJsonError(40003);
            return;
        }
    }


    public void save(){
        TmBrandMaterials user = (TmBrandMaterials) getReqBodyObject(TmBrandMaterials.class);
        user.setStatus(1);
        user.setCreateTime(new Date());
        user.setApplyTime(new Date());
        if(user.save()){
            renderJsonRet(user.getId());
        }else{
            renderJsonError(40003);
        }
    }
}
